<?php

include_once('admin.php');

/**
 * The Backup Management Class provides functions pertaining to database backup
 * @package admin
 */
class backup_management extends admin
{
	function __construct()
	{
		// calls the parent constructor     
		parent::__construct();
		
		// Redirect, if not logged in as admin
		if ( !$this->session->userdata('CurrentUser') || !$this->session->userdata('CurrentUser')->su_access )
			redirect('authenticate/login');
	}
	
	/**
     * Displays page giving the user options for choosing their preffered method of backup(public or intranet)
     */
	public function index( )
	{
		// Loads the "backup_database" view
     // loads one page e.g. backup_database page. 
		$this->data['main_content'] = $this->load->view('admin/backup_management/backup_management', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	/**
     * Backs up the following tables: tbl_location, tbl_volume, tbl_author, tbl_subject, tbl_author_volume and tbl_subject_volume
     */
	public function backupForPublic( )
	{
		// Load the DB utility class
		$this->load->dbutil();
		
                // Preferences for backup
		$prefs = array(
				'tables'	=> array(	'tbl_location',
										'tbl_volume',
										'tbl_author',
										'tbl_subject',
										'tbl_author_volume',
										'tbl_subject_volume'),  // Array of tables to backup.
                'format'      => 'zip',             // gzip, zip, txt
                'add_drop'    => TRUE,              // Whether to add DROP TABLE statements to backup file
                'add_insert'  => TRUE,              // Whether to add INSERT data to backup file
                'newline'     => "\n"               // Newline character used in backup file
              );

		$backup =& $this->dbutil->backup($prefs);
		
		// Load the download helper and send the file to your desktop
		$this->load->helper('download');
		force_download('public_backup.zip', $backup); 
	}
	
	/**
     * Backs up all tables
     */
	public function backupForIntranet( )
	{
		// Load the DB utility class
		$this->load->dbutil();
		
		$prefs = array(
                'format'      => 'zip',             // gzip, zip, txt
                'add_drop'    => TRUE,              // Whether to add DROP TABLE statements to backup file
                'add_insert'  => TRUE,              // Whether to add INSERT data to backup file
                'newline'     => "\n"               // Newline character used in backup file
              );
     // Call dbutil->backup function and return SQL Strings.
		$backup =& $this->dbutil->backup($prefs);
      
		
		// Load the download helper and send the file to your desktop
		$this->load->helper('download');
		force_download('intranet_backup.zip', $backup); 
	}
}

?>